/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeeAddEditPnl.java
 *
 * Created on Jul 10, 2011, 2:14:05 PM
 */
package com.viweb.storemanager.ui.employee;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Employee;
import com.viweb.storemanager.ui.customer.CustomerAddEditPnl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class EmployeeAddEditPnl extends javax.swing.JPanel {

    private EmployeePnl parent;
    private Employee employee;

    /** Creates new form EmployeeAddEditPnl */
    public EmployeeAddEditPnl() {
        initComponents();
    }

    public EmployeeAddEditPnl(EmployeePnl parent) {
        initComponents();
        this.parent = parent;
    }

    public void assignEmployee(Employee employee) {
        this.employee = employee;

        if (this.employee.getId() != -1) {
            this.txtName.setText(employee.getName());
            this.txtAddress.setText(employee.getAddress());
            this.txtPhone.setText(employee.getPhone());
            this.txtUsername.setText(employee.getUsername());
            this.txtPassword.setText(employee.getPassword());
            //TODO: quyen
            this.txtNotes.setText(employee.getNote());
            this.lblDateModifier.setText(null);
        } else {
            this.txtName.setText(null);
            this.txtAddress.setText(null);
            this.txtPhone.setText(null);
            //TODO: ngay vao lam
            this.txtUsername.setText(null);
            this.txtPassword.setText(null);
            //TODO: quyen
            this.txtNotes.setText(null);
            this.lblDateModifier.setText(null);
        }
    }

    public void setEditableMode(boolean editable) {
        if (editable) {
            this.txtName.requestFocus();
        }
        this.txtName.setEditable(editable);
        this.txtAddress.setEditable(editable);
        this.txtPhone.setEditable(editable);
        this.txtUsername.setEditable(editable);
        this.txtPassword.setEditable(editable);
        this.txtNotes.setEditable(editable);
        this.btnSave.setEnabled(editable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupStatus = new javax.swing.ButtonGroup();
        btnSave = new javax.swing.JButton();
        pnlEmployee = new javax.swing.JPanel();
        lblEmp = new javax.swing.JLabel();
        sep1 = new javax.swing.JSeparator();
        pnlAccountInfo = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        lblRules = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        cbxRules = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        lblAccount = new javax.swing.JLabel();
        sep2 = new javax.swing.JSeparator();
        lblName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        lblStartDate = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtPhone = new javax.swing.JTextField();
        lblNote = new javax.swing.JLabel();
        scpNote = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextPane();
        lblEmpty = new javax.swing.JLabel();
        lblDateModifier = new javax.swing.JLabel();
        lblAfterDateModifier = new javax.swing.JLabel();
        dtcDate = new com.toedter.calendar.JDateChooser();
        radioYes = new javax.swing.JRadioButton();
        lblStatus = new javax.swing.JLabel();
        radioNo = new javax.swing.JRadioButton();

        setMinimumSize(new java.awt.Dimension(518, 195));
        setPreferredSize(new java.awt.Dimension(518, 195));
        setLayout(new java.awt.GridBagLayout());

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/save.png"))); // NOI18N
        btnSave.setText("Lưu");
        btnSave.setToolTipText("Lưu thông tin thêm mới hay hiệu chỉnh nhân viên");
        btnSave.setEnabled(false);
        btnSave.setMaximumSize(new java.awt.Dimension(80, 26));
        btnSave.setMinimumSize(new java.awt.Dimension(80, 26));
        btnSave.setPreferredSize(new java.awt.Dimension(80, 26));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(btnSave, gridBagConstraints);

        pnlEmployee.setLayout(new java.awt.GridBagLayout());

        lblEmp.setText("Thông tin nhân viên");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        pnlEmployee.add(lblEmp, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        pnlEmployee.add(sep1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(pnlEmployee, gridBagConstraints);

        pnlAccountInfo.setLayout(new java.awt.GridBagLayout());

        lblUsername.setText("Tên đăng nhập:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
        pnlAccountInfo.add(lblUsername, gridBagConstraints);

        lblPassword.setText("Mật khẩu:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
        pnlAccountInfo.add(lblPassword, gridBagConstraints);

        lblRules.setText("Quyền:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
        pnlAccountInfo.add(lblRules, gridBagConstraints);

        txtUsername.setEditable(false);
        txtUsername.setMinimumSize(new java.awt.Dimension(150, 25));
        txtUsername.setPreferredSize(new java.awt.Dimension(150, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        pnlAccountInfo.add(txtUsername, gridBagConstraints);

        txtPassword.setEditable(false);
        txtPassword.setMinimumSize(new java.awt.Dimension(150, 25));
        txtPassword.setPreferredSize(new java.awt.Dimension(150, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        pnlAccountInfo.add(txtPassword, gridBagConstraints);

        cbxRules.setEnabled(false);
        cbxRules.setMinimumSize(new java.awt.Dimension(150, 25));
        cbxRules.setPreferredSize(new java.awt.Dimension(150, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        pnlAccountInfo.add(cbxRules, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        lblAccount.setText("Tài khoản đăng nhập");
        jPanel3.add(lblAccount, new java.awt.GridBagConstraints());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel3.add(sep2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        pnlAccountInfo.add(jPanel3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 30, 5);
        add(pnlAccountInfo, gridBagConstraints);

        lblName.setText("Họ tên:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblName, gridBagConstraints);

        lblAddress.setText("Địa chỉ:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblAddress, gridBagConstraints);

        lblPhone.setText("Số điện thoại:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblPhone, gridBagConstraints);

        lblStartDate.setText("Ngày làm việc:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblStartDate, gridBagConstraints);

        txtName.setEditable(false);
        txtName.setMinimumSize(new java.awt.Dimension(200, 25));
        txtName.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtName, gridBagConstraints);

        txtAddress.setEditable(false);
        txtAddress.setMinimumSize(new java.awt.Dimension(200, 25));
        txtAddress.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtAddress, gridBagConstraints);

        txtPhone.setEditable(false);
        txtPhone.setMinimumSize(new java.awt.Dimension(200, 25));
        txtPhone.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtPhone, gridBagConstraints);

        lblNote.setText("Ghi chú:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblNote, gridBagConstraints);

        scpNote.setPreferredSize(new java.awt.Dimension(8, 22));

        txtNotes.setEditable(false);
        scpNote.setViewportView(txtNotes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(scpNote, gridBagConstraints);

        lblEmpty.setMinimumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setPreferredSize(new java.awt.Dimension(5, 50));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblEmpty, gridBagConstraints);

        lblDateModifier.setText("Ngày cập nhật:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 0);
        add(lblDateModifier, gridBagConstraints);

        lblAfterDateModifier.setEnabled(false);
        lblAfterDateModifier.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        lblAfterDateModifier.setMinimumSize(new java.awt.Dimension(200, 14));
        lblAfterDateModifier.setPreferredSize(new java.awt.Dimension(200, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lblAfterDateModifier, gridBagConstraints);

        dtcDate.setDateFormatString("d MMM, yyyy");
        dtcDate.setMinimumSize(new java.awt.Dimension(200, 25));
        dtcDate.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(dtcDate, gridBagConstraints);

        btnGroupStatus.add(radioYes);
        radioYes.setText("Có");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioYes, gridBagConstraints);

        lblStatus.setText("Tình trạng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblStatus, gridBagConstraints);

        btnGroupStatus.add(radioNo);
        radioNo.setText("Không");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 0, 5);
        add(radioNo, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            this.employee.setName(this.txtName.getText().trim());
            this.employee.setAddress(this.txtAddress.getText().trim());
            //this.customer.setType((DrinkType) this.cmbDrinkType.getSelectedItem());
            this.employee.setPhone(this.txtPhone.getText().trim());
            this.employee.setUsername(this.txtUsername.getText().trim());
            this.employee.setPassword(String.valueOf(this.txtPassword.getPassword()).trim());
            this.employee.setNote(this.txtNotes.getText().trim());

            boolean status = false;
            if (this.employee.getId() != -1) {
                status = Biz.getInstance().insertEmployee(employee);
            } else {
                status = Biz.getInstance().updateEmployee(employee);
            }
            // Nếu cập nhật (thêm mới hoặc sữa thông tin) không thành công
            if (status) {
                OptionPane.show(JOptionPane.getFrameForComponent(EmployeeAddEditPnl.this), "Cập nhật không thành công! " + Biz.getInstance().getLastError(), "Thông báo", OptionPane.MsgTYPE_WARNING);
                // Nếu cập nhật thành công
            } else {
                if (this.employee.getId() == -1) {
                    // Set id cho quốc gia vừa mới cập nhật (chỉ có ý nghĩa trong trường hợp thêm mới)
                    //this.customer.setId(id);
                    OptionPane.show(JOptionPane.getFrameForComponent(EmployeeAddEditPnl.this), "Thêm mới nhân viên thành công!", "Thông báo");
                } else {
                    OptionPane.show(JOptionPane.getFrameForComponent(EmployeeAddEditPnl.this), "Cập nhật nhân viên thành công!", "Thông báo");
                }
                this.parent.updateComponentsUI(this.employee);
            }
            CursorManager.setDefaultCursor(this);
        } catch (Exception ex) {
            Logger.getLogger(CustomerAddEditPnl.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroupStatus;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbxRules;
    private com.toedter.calendar.JDateChooser dtcDate;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblAccount;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblAfterDateModifier;
    private javax.swing.JLabel lblDateModifier;
    private javax.swing.JLabel lblEmp;
    private javax.swing.JLabel lblEmpty;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblRules;
    private javax.swing.JLabel lblStartDate;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPanel pnlAccountInfo;
    private javax.swing.JPanel pnlEmployee;
    private javax.swing.JRadioButton radioNo;
    private javax.swing.JRadioButton radioYes;
    private javax.swing.JScrollPane scpNote;
    private javax.swing.JSeparator sep1;
    private javax.swing.JSeparator sep2;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextPane txtNotes;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
